package com.sweet.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sweet.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfo_Global_Fallback")
public class OrderController {
    @Autowired
    private IOrderService orderService;


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        log.info("id:{}, Thread: {}", id, Thread.currentThread().getName());
        return orderService.paymentInfo_OK(id);
    }

    // 超时
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOut_Fallback",commandProperties = {
            // 3秒钟以内就是正常的业务逻辑
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    }
    )
    @GetMapping("/consumer/payment/hystrix/timeout/{id}/{time}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id,
                                      @PathVariable("time") Integer time) {
        log.info("id:{}, Thread: {}", id, Thread.currentThread().getName());
        try {
            // 模拟消费端服务超时
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return orderService.paymentInfo_TimeOut(id, time);
    }

    /**
     * 超时兜底方法
     * @param id
     * @return
     */
    public String paymentInfo_TimeOut_Fallback(Integer id, Integer time) {
        return "线程池：" + Thread.currentThread().getName() + "   paymentInfo_TimeOut,"+ time + "s, id：  " + id + "\t" + "消费端服务超时或异常，请稍后再试~~";
    }


    // 模拟运行时异常失败
    /*@HystrixCommand(fallbackMethod = "paymentInfo_Error_Fallback", commandProperties = {
            // 3秒钟以内就是正常的业务逻辑
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    }
    )*/
    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/error/{id}")
    public String paymentInfo_Error(@PathVariable("id") Integer id) {
        log.info("id:{}, Thread: {}", id, Thread.currentThread().getName());
        // 模拟消费端异常
        System.out.println(1/0);
        return orderService.paymentInfo_Error(id);
    }

    /**
     * 服务异常兜底方法
     * @param id
     * @return
     */
    /*public String paymentInfo_Error_Fallback(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "   paymentInfo_Error, id：  " + id + "\t" + "消费端服务超时或异常，请稍后再试~~";
    }*/


    /**
     * 全局兜底方法
     * @return
     */
    public String paymentInfo_Global_Fallback() {
        return "线程池：" + Thread.currentThread().getName() + "   paymentInfo_Error~, Global fallback!";
    }
}
